home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / auxcfg.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  3.7 KB  |  135 lines

  1. /*
  2.   This include file should be used when accessing auxillary devices.
  3.   Routines defined:
  4.      int auxopen(name) char name[]
  5.          auxerr(string)
  6.   auxopen returns a file descriptor for use with read and write.
  7.   The global array "auxparams" is used to store device specific information.
  8.   auxerr should be called, whenever the device returns an error message.
  9. */
  10.  
  11. char *auxerrprg = NULL,
  12.      *auxerrmsg = NULL,
  13.      *auxdatafile = NULL,
  14.      *auxparams[20];
  15.  
  16. auxopen(name)
  17. char name[];
  18. {
  19. int i,n,m,fd;
  20. int id;
  21. char c,*s, *z;
  22. char *tty;
  23. FILE *fp;
  24. char *home;
  25.  
  26.    s = (char *) malloc(80);
  27.    z = (char *) malloc(80);
  28.    tty = (char *) malloc(80);
  29.    auxerrprg = (char *) malloc(80);
  30.    auxerrmsg = (char *) malloc(80);
  31.    auxdatafile = (char *) malloc(80);
  32.    for(n=0;n<20;n++) {
  33.       auxparams[n] = (char *) malloc(80);
  34.       strcpy(auxparams[n],"");
  35.    }
  36. #ifdef UNIX
  37. /* get name of tty */
  38.    system("whoami >/usr/tmp/whoami");
  39.    fp = fopen("/usr/tmp/whoami","r");
  40.    fgets(tty,80,fp); for(i=0;i<strlen(tty);i++) if(tty[i]==10) tty[i]=0;
  41.    fclose(fp); unlink("/usr/tmp/whoami");
  42. #endif
  43.    fp=fopen("Aux_Config","r");
  44. #ifdef UNIX
  45.    if(fp==NULL) {
  46.       home = (char *) getenv("HOME");
  47.       s[0]=0;
  48.       if(home != NULL) {strcpy(s,home); strcat(s,"/"); }
  49.       strcat(s,"Aux_Config");
  50.       fp=fopen(s,"r");
  51.    }
  52.    if(fp==NULL) {
  53.       home = (char *) getenv("LISEPRG");
  54.       s[0]=0;
  55.       if(home != NULL) {strcpy(s,home); strcat(s,"/"); }
  56.       strcat(s,"Aux_Config");
  57.       fp=fopen(s,"r");
  58.    }
  59. #endif
  60. #ifdef AMIGA
  61.    if(fp==NULL) fp=fopen("LISE:Aux_Config","r");
  62. #endif
  63.    if(fp==NULL) {
  64.       fprintf(stderr,"auxopen: can not find Aux_Config file\n");
  65.       exit(-1);
  66.    }
  67.    while(!feof(fp)) {
  68.       c=fgetc(fp);
  69.       if(c==';') while(c!=10) c=fgetc(fp);            /* skip comment */
  70.       if(c==':') {                                    /* new entry */
  71.          fscanf(fp,"%s\n",s);
  72.          if(strcmp(s,name)==0) {                      /* entry found */
  73.             n=0; m=0;
  74.             while(c!='#') {
  75.                 c=fgetc(fp);
  76.                 if(c=='#') break;
  77.                 if(c==10) {
  78.                   auxparams[n++][m]=0;
  79.                   m=0;
  80.                   continue;
  81.                }
  82.                if(c==';') {                           /* skip comment */
  83.                   while(c!=10) c=fgetc(fp);
  84.                   auxparams[n++][m]=0;
  85.                   m=0;
  86.                   continue;
  87.                } else {                               /* store string */
  88.                   auxparams[n][m++]=c;
  89.                }
  90.             }
  91.          } else {                                     /* not the right entry */
  92.             while(c!='#') {
  93.                while(c!=10) c=fgetc(fp);
  94.                c=fgetc(fp);
  95.             }
  96.          }
  97.       }
  98.    }
  99.    fclose(fp);
  100.    if(auxparams[0][0]==0) {
  101.       fprintf(stderr,"sorry, device entry >%s< not found in Aux_Config\n",name);
  102.       exit(-1);
  103.    }
  104.    for(n=0;n<20;n++) {
  105.       for(m=strlen(auxparams[n])-1;(auxparams[n][m]==32);m--) auxparams[n][m]=0;
  106.       m = 0; while(auxparams[n][m]==32) m++;
  107.       i = 0; while(auxparams[n][m]!=0) auxparams[n][i++] = auxparams[n][m++];
  108.       auxparams[n][i++] = 0;
  109. #ifdef UNIX
  110.       i = instr("@",auxparams[n]);
  111.       if(i >= 0) {
  112.          s[0] = 0;
  113.          midstr(s,auxparams[n],0,i - 1);
  114.          strcat(s,tty);
  115.          midstr(z,auxparams[n],i + 1,strlen(auxparams[n]));
  116.          strcat(s,z); strcpy(auxparams[n],s);
  117.       }
  118. #endif      
  119.    }
  120.    fd=open(auxparams[0],2,0);
  121.    strcpy(auxerrprg,auxparams[1]);
  122.    strcpy(auxerrmsg,auxparams[2]);
  123.    strcpy(auxdatafile,auxparams[3]);
  124.    free(s); free(z); free(tty);
  125.    return(fd);
  126. }
  127.  
  128. void auxerr(string)
  129. char string[];
  130. {
  131.    system(auxerrprg);
  132.    fprintf(stderr,"%s  :  %s\n",auxerrmsg,string);
  133. }
  134.  
  135.